<?
	//Coding by trongcoder
	/*
		//Extention :::::
			
			//create PathFile in config.php 
			//folder "upload/" set at index AND CHMOD777
			
				$PathFile = "upload/";
				$PathThumbFile = "upload/thumb/";
				$imgWidth = 120;
				$imgHeight = 120;
				
			//
		//
		
		
		
		
		//Create $upload at index.php
		
			$upload = new upload();
			
		//Help upload type ADD
		
		$DuongDanAnh = $upload->upload_file($PathFile,'DuongDanAnh');
		if($DuongDanAnh != ""){
			$filetype = $upload->file_type($DuongDanAnh);
			$upload->crop_image($imgWidth,$imgHeight,$PathFile . $DuongDanAnh,$filetype,$PathThumbFile .$DuongDanAnh);
		}	
		
		
		
		//Help Update type EDIT
		
		$DuongDanAnh_tmp = $upload->upload_file($PathFile,'DuongDanAnh');
		if($DuongDanAnh_tmp != ''){
			$filetype = $upload->file_type($DuongDanAnh_tmp);
			$upload->crop_image($imgWidth,$imgHeight,$PathFile . $DuongDanAnh_tmp,$filetype,$PathThumbFile .$DuongDanAnh_tmp);
			$DuongDanAnh = "'" . $DuongDanAnh_tmp . "'";
		}else{
			$DuongDanAnh = "`DuongDanAnh`";
		}	
	*/
	class upload{
		//Viet ham upload
		function upload_file($file_path,$files){
			$fname = $_FILES[''.$files.'']['name'];
			//Get extention file
			$getExt = explode ('.', $fname);
			$file_ext = $getExt[count($getExt)-1];
			//set file name
			$rand_name = "dvt_".time();
			$fileName = $rand_name .".". $file_ext;
			//
			$fileType = $_FILES[''.$files.'']['type'];
			$filePath = $file_path.$fileName;
			if ($fileName == NULL){
				$fileName = "";
				
			}else if (($fileName != NULL) && (!file_exists($_FILES[''.$files.''])) && (($fileType == "application/octet-stream") || ($fileType == "application/x-php") || ($fileType == "text/html"))){
				
					$fileName = "";
					
			}else if (move_uploaded_file($_FILES[''.$files.'']['tmp_name'],$filePath)){
				$fileName = $fileName; 
			}else {
				
				$fileName = "";
			}
			
			return $fileName;
		}


		//delete file
		function fdelete($fpname){
				if(file_exists($fpname)){
					unlink($fpname);
				}
		}
		
		//$filetype = File_Type($filename);
		//crop_image(100,100,'../uploads/'.$filename,$filetype,'upload/thumb/'.$filename);
		function crop_image($nw, $nh, $source, $stype, $dest)	// Crop images resize
			{	 
			   $size = getimagesize($source);
				  $w = $size[0];
				  $h = $size[1];
				  switch($stype)
				  {				
					  
					  case 'gif':
					  	$simg = imagecreatefromgif($source);
					  	break;			 
					  case 'jpg':
					  	$simg = imagecreatefromjpeg($source);
					  	break;
					  case 'png':
					  	$simg = imagecreatefrompng($source);
					  	break;
					  default:
					  		echo "<script>alert('No support this type file ! (Support: gif,jpg,png)');</script>";
							exit();
					  	break;
				  }       
				  $dimg = imagecreatetruecolor($nw, $nh);       
				  $wm = $w/$nw;
				  $hm = $h/$nh;       
				  $h_height = $nh/2;
				  $w_height = $nw/2;      
				  if($w> $h) {       
					  $adjusted_width = $w / $hm;
					  $half_width = $adjusted_width / 2;
					  $int_width = $half_width - $w_height;       
					  imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
				  } elseif(($w <$h) || ($w == $h)) {
					  $adjusted_height = $h / $wm;
					  $half_height = $adjusted_height / 2;
					  $int_height = $half_height - $h_height;       
					  imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);   
				  } else {
					  imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
				  }
				  imagejpeg($dimg,$dest,100);
			}


			function file_type($filename)	// Lay ra kieu file
			{
				$path_info = pathinfo($filename);
				return strtolower($path_info['extension']);
			}
		
		
	}
?>